package com.asset.dispoal.gbEvidenceIdentification.service.commom;

import com.asset.dispoal.commom.page.PagingQuery;
import com.asset.dispoal.gbEvidenceIdentification.domain.db.entity.GbEvidenceIdentificationDo;
import com.asset.dispoal.gbEvidenceIdentification.domain.db.mapper.GbEvidenceIdentificationMapper;
import com.asset.dispoal.gbEvidenceIdentification.dto.GbEvidenceIdentificationRequest;
import com.asset.dispoal.gbEvidenceIdentification.dto.GbEvidenceIdentificationResponse;
import com.asset.dispoal.gbEvidenceIdentification.service.converter.GbEvidenceIdentificationConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tech.mhuang.pacebox.core.ddd.BaseCommand;
import tech.mhuang.pacebox.springboot.core.local.GlobalHeaderThreadLocal;
import tech.mhuang.pacebox.springboot.protocol.Result;
import tech.mhuang.pacebox.springboot.protocol.data.Page;
import tech.mhuang.pacebox.springboot.protocol.data.PageVO;

import java.util.List;

/**
 * @author qr
 * @date 2022/3/3$
 */
@Component
public class QueryEvidenceIdentificationCommand implements BaseCommand<GbEvidenceIdentificationRequest, Result<PageVO<GbEvidenceIdentificationResponse>>> {


    @Autowired
    GbEvidenceIdentificationMapper gbEvidenceIdentificationMapper;

    @Override
    public Result<PageVO<GbEvidenceIdentificationResponse>> execute(GbEvidenceIdentificationRequest request) {
        final GbEvidenceIdentificationDo gbEvidenceIdentificationDo = GbEvidenceIdentificationConverter.INSTANCE.reqToDo(request);
        gbEvidenceIdentificationDo.setPermission((String) GlobalHeaderThreadLocal.getOrException().getExtraMap().get("permission"));
        final int count = gbEvidenceIdentificationMapper.count(gbEvidenceIdentificationDo);
        final Page<GbEvidenceIdentificationDo> page = PagingQuery.getPage(count, gbEvidenceIdentificationDo);
        PageVO<GbEvidenceIdentificationResponse> pageVO = new PageVO<>();
        pageVO.setTotalSize(count);
        if (page != null) {
            final List<GbEvidenceIdentificationDo> evidenceIdentificationDos = gbEvidenceIdentificationMapper.page(page);
            final List<GbEvidenceIdentificationResponse> gbEvidenceIdentificationResponses = GbEvidenceIdentificationConverter.INSTANCE.doToRes(evidenceIdentificationDos);
            pageVO.setResult(gbEvidenceIdentificationResponses);
        }
        return Result.ok(pageVO);
    }
}
